package com.yc.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.yc.bean.User;
import com.yc.biz.UserBiz;
import com.yc.biz.impl.UserBizImp1;

/**
 * @author Huathy
 * 这个类包含所有与管理员端相关的请求操作
 */
@WebServlet("/admin_user")
public class Admin_UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	UserBiz ub = new UserBizImp1();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String op = req.getParameter("op");
		PrintWriter out = resp.getWriter();
		if( "getUserByPage".equals(op) ){
			getUserByPage(req,resp,out);
		}else if ("adminLogin".equals(op)) {
			adminLogin(req, resp, out);
		} else if ("user_modify".equals(op)) {
			user_modify(req, resp, out);
		}else if ("user_del".equals(op)) {
			user_del(req, resp, out);
		}else if ("person_modify".equals(op)) {
			person_modify(req, resp, out);
		}
	}

	//用户分页查询
	private void getUserByPage(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int totalPage = 0;
		try {
			totalPage = Integer.parseInt(req.getParameter("userTotalPage"));
		} catch (NumberFormatException e) {
//			e.printStackTrace();
			totalPage = ub.getUserTotalPage();
			req.getSession().setAttribute("userTotalPage", totalPage);
		}
		int page = 1;
		try {
			page = Integer.parseInt( req.getParameter("page") );
		} catch (NumberFormatException e) {
//			e.printStackTrace();
		}
		if(page>totalPage){
			page=totalPage;
		}
		if(page<1){
			page = 1;
		}
		//注意:这里两个if判断先后顺序不可更换。totolpage最开始为0
		List<User> users = ub.getUsersbyPage(page);
//		Gson gson = new Gson();
		req.getSession().setAttribute("users", users);
		if(users.isEmpty()){
			out.print(-1);
			return;
		}
		out.print(page);
	}

	//管理员用户删除
	private void user_del(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid = -1;
		try {
			uid = Integer.parseInt( req.getParameter("uid") );
		} catch (NumberFormatException e) {
//			e.printStackTrace();
			out.print(-1);
			return;
		}
		int result = ub.user_del(uid);
		//req.getSession().removeAttribute("userTotalPage");
		out.print(result);
	}
	
	//管理员用户修改
	private void user_modify(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid = -1;
		String uname = null;
		String tel = null;
		try {
			uid = Integer.parseInt( req.getParameter("uid") );
			uname = req.getParameter("uname");
			tel = req.getParameter("tel");
		} catch (Exception e) {
//			e.printStackTrace();
			out.print(-1);
			return;
		}
		int result = ub.modify_user(uid,uname,tel);
		out.print(result);
	}
	
	//管理员个人信息修改
	private void person_modify(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		int uid = -1;
		String uname = null;
		String pwd = null;
		String tel = null;
		try {
			uid = Integer.parseInt( req.getParameter("uid") );
			uname = req.getParameter("uname");
			pwd = req.getParameter("pwd");
			tel = req.getParameter("tel");
		} catch (Exception e) {
//			e.printStackTrace();
			out.print(-1);
			return;
		}
		int result = ub.modify_persons(uid,uname,pwd,tel);
		out.print(result);
	}
	

	//管理员登录
	private void adminLogin(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
		String uname = req.getParameter("uname");
		String pwd = req.getParameter("pwd");
		User user = ub.adminLogin(uname, pwd);
		if (user == null) {
			out.print(0);
		} else {
			// 存session
			req.getSession().setAttribute("admin", user);
			out.print(1);
		}
	}

}
